package common.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class DirHandle {	
	/**
	 * @遍历某目录下面的文件,(隐藏文件除外)  注:未过滤含+号等的记录
	 * @param path: 需要查找的顶级目录
	 * @param hs:把查找的目标存入set
	 * @param start: 查找出以start开头的字符串
	 * @param end: 查找出以end结尾的字符串
	*/
	public static String JSP_DIR = "F:\\MyEclipseWorkspace\\mobilemall\\WebRoot\\jsp";
	public static String JSP_TXT_DIR = "E:\\jsp.txt";
	public static String ACTION_TXT_DIR = "E:\\action.txt";
	public static String PROJECT_ROOT = "/mobilemall";
	public static String RPOJECT_NAME = "mobilemall";
	public void fetchDirAction(String path,HashSet<String> hs,String start,String end) throws IOException{
		File fp = new File(path);
		File[] files = fp.listFiles();
		if(null != files){
			for(File file : files){
				if(!file.isDirectory()&&!file.isHidden()){
					String fname = file.getName();
					int len = file.getName().length();
					if(!fname.substring(len-4, len).equals(".jsp")&&!fname.substring(len-3, len).equals(".js"))
						continue;//如果还是jsp或js则返回
					FileInputStream fis = new FileInputStream(file);
					InputStreamReader isr = new InputStreamReader(fis);
					BufferedReader br = new BufferedReader(isr);
					String line = null;
					while((line=br.readLine())!=null){
						int ind=-1;
						int ond=-1;
						String str = null;
						if((ind=line.indexOf(start))>-1 && (ond=line.indexOf(end))>-1){
							str = line.substring(ind, ond+end.length());
							hs.add(str);
						}
					}
					br.close();
				}
				if(file.isDirectory()&&!file.isHidden()){
					fetchDirAction(file.getPath(),hs,start,end);
				}
			}
		}
	}
	/**
	 * @遍历jsp目录（隐藏文件除外）
	 * @param path: 需要查找的顶级目录
	 * @param hs:把查找的目标存入set
	*/
	public void fetchDirJsp(String path,HashSet<String> hs) throws IOException{
		File fp = new File(path);
		File[] files = fp.listFiles();
		if(null != files){
			for(File file : files){
				if(!file.isDirectory()&&!file.isHidden()){
					int len = file.getName().length();
					if(file.getName().substring(len-4, len).equals(".jsp"))
						hs.add(PROJECT_ROOT + file.getPath().substring(file.getPath().indexOf("jsp")-1).replace("\\", "/"));//在这里根据情况截取jsp路径
				}
				if(file.isDirectory()&&!file.isHidden()){
					fetchDirJsp(file.getPath(),hs);
				}
			}
		}
	}
	public final List<File> generateReourceFile() throws Exception{
		// 生成jsp.txt
		File fi1 = new File(JSP_TXT_DIR);
		FileOutputStream fos1 = new FileOutputStream(fi1);
		BufferedWriter cloud_ifw1 = new BufferedWriter(new OutputStreamWriter(fos1));		
		HashSet<String> hs1 = new HashSet<String>();		
		fetchDirJsp(JSP_DIR,hs1);
		for(Iterator<String> ite = hs1.iterator();ite.hasNext();){
			String str = ite.next();
			cloud_ifw1.write(str);
			cloud_ifw1.newLine();
		}
		cloud_ifw1.flush();
		cloud_ifw1.close();
		
		// 生成action.txt
		File fi2 = new File(ACTION_TXT_DIR);
		FileOutputStream fos2 = new FileOutputStream(fi2);
		BufferedWriter cloud_ifw2 = new BufferedWriter(new OutputStreamWriter(fos2));		
		HashSet<String> hs2 = new HashSet<String>();		
		fetchDirAction(JSP_DIR,hs2,PROJECT_ROOT,".action");
		for(Iterator<String> ite = hs2.iterator();ite.hasNext();){
			String str = ite.next();
			cloud_ifw2.write(str);
			cloud_ifw2.newLine();
		}
		cloud_ifw2.flush();
		cloud_ifw2.close();
		
		List<File> fl = new ArrayList<File>();
		fl.add(fi1);
		fl.add(fi2);
		return fl;
		
	}
	/*
	public static void main(String arg[]) throws IOException{
		DirHandle is = new DirHandle();
//		File fil = new File("E:\\action.txt");
		File fil = new File("E:\\jsp.txt");
		FileOutputStream fos = new FileOutputStream(fil);
		BufferedWriter cloud_ifw = new BufferedWriter(new OutputStreamWriter(fos));		
		HashSet<String> hs = new HashSet<String>();
//		is.fetchDirAction("E:\\workspace\\kyxt\\WebRoot\\jsp",hs,"/kyxt",".action");
		is.fetchDirJsp("F:\\MyEclipseWorkspace\\mobilemall\\WebRoot\\jsp",hs);
		for(Iterator<String> ite=hs.iterator();ite.hasNext();){
			String str = ite.next();
			cloud_ifw.write(str);
			cloud_ifw.newLine();
		}
		cloud_ifw.flush();
		cloud_ifw.close();
	}*/
}
